﻿#include "addremind.h"
#include "ui_addremind.h"

AddRemind::AddRemind(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddRemind)
{
    ui->setupUi(this);
}


AddRemind::~AddRemind()
{
    delete ui;
}


bool AddRemind::addItem(const QString &label, const QString &detail)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (topic,label,detail) values (?,?,?)")
                  .arg(TD_TRIGGER));

    query.addBindValue(T_REMIND);
    query.addBindValue(label);
    query.addBindValue(detail);
    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

void AddRemind::on_btn_submit_clicked()
{
    QString kw=ui->kw->text().trimmed();
    if(kw.size() < 2)
    {
        QMessageBox::warning(this,QStringLiteral("输入无效"),
                             QStringLiteral("关键字不能少于2个字符！"));
        return;
    }

    QString detial=ui->detail->toPlainText().trimmed();
    if(detial.isEmpty())
    {
        MESSAGE_DETAIL_EMPTY
    }

    if(addItem(kw,detial))
        accept();
    else
        MESSAGE_CANNOT_SUBMIT
}
